/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package javautil.stat.desc;

import javautil.collections.DimensionCheck;

/**
 *
 * @author stewari1
 */
public class SampleTotal {

    /**
     * summing across the (first) sampling dimension
     * @param data
     * @return
     */
    public static int[] getSampleTotal(int[][] data) {

        if (!DimensionCheck.isSameDimen(data)) {
            throw new Error("Dimensions are not the same");
        }

        int[] result = new int[data[0].length];

        for (int i = 0; i < result.length; i++) {

            for (int h = 0; h < data.length; h++) {

                result[i] += data[h][i];
            }
        }

        return result;
    }

    /**
     * summing accross the first dimension (sample dimension(h)).
     * @param arrays
     * @return
     */
    public static int[][] getSampleTotal(int[][][] data) {

        if (!DimensionCheck.isSameDimen(data)) {
            throw new Error("Dimensions are not the same");
        }

        int[][] result = new int[data[0].length][data[0][0].length];

        for (int i = 0; i < data[0].length; i++) {

            for (int j = 0; j < data[0][0].length; j++) {

                for (int h = 0; h < data.length; h++) {

                    result[i][j] += data[h][i][j];
                }
            }
        }

        return result;
    }
}
